package yunquan.wang.modules.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.dao.MatchType;
import yunquan.wang.base.dao.QueryHelper;
import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.service.impl.BaseServiceImpl;
import yunquan.wang.base.web.TreePojo;
import yunquan.wang.modules.base.dao.AuthorityDao;
import yunquan.wang.modules.base.dao.AuthorityTypeDao;
import yunquan.wang.modules.base.entity.Authority;
import yunquan.wang.modules.base.entity.AuthorityType;
import yunquan.wang.modules.base.service.AuthorityService;
/**
 *  Authority Service层 实现
 *  @author 王云权  wangyunquan@gmail.com
 *  用生成器 生成于 2011-09-30 22:37
 *
 */
@Service("authorityService")
@Transactional(value="jpaTransaction")
public class AuthorityServiceImpl extends BaseServiceImpl<Authority>
		implements AuthorityService {
	private AuthorityDao authorityDao;
	private AuthorityTypeDao authorityTypeDao;
	@Resource(name="authorityDao")
	public void setDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}
	@Resource(name="authorityTypeDao")
	public void setDao(AuthorityTypeDao authorityTypeDao) {
		this.authorityTypeDao = authorityTypeDao;
	}

	public BaseRepository<Authority, String> getDao() {
		return authorityDao;
	}

	@Override
	public List<TreePojo> getAuthorityTree() {
		List<TreePojo> list = new ArrayList<TreePojo>();
		AuthorityType top=	authorityTypeDao.findOne(QueryHelper.filter("name", MatchType.EQ, "A"));
		list.addAll(getSubAuthorityType(top,"0"));
		return list;
	}
	private List<TreePojo> getSubAuthorityType(AuthorityType authType,
			String parentId) {
		List<TreePojo> list = new ArrayList<TreePojo>();
		TreePojo topNode = new TreePojo(authType.getId(), parentId,
				authType.getCnname());
		topNode.setOpen(true);
		list.add(topNode);
		for(Authority auth:authType.getAuthoritys())
		{
			TreePojo node = new TreePojo(auth.getId(), authType.getId(),
					auth.getAuthinfo());
			list.add(node);
		}
		for (AuthorityType child : authType.getChilds()) {
			list.addAll(getSubAuthorityType(child, authType.getId()));
		}
		return list;
	}
}
